package com.edu.neusoft.student.controller;

import com.edu.neusoft.student.domain.Orderdetail;
import com.edu.neusoft.student.service.OrderdetailService;
import com.edu.neusoft.student.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/orderdetails")
@Api(tags = "Order Detail Management API", description = "Operations related to order detail management")
@Validated
public class OrderdetailController {

    @Autowired
    private OrderdetailService orderdetailService;

    // 获取订单详情通过ID
    @GetMapping("/{orderdetailid}")
    @ApiOperation(value = "Get order detail by ID", notes = "Retrieve a specific order detail by its ID")
    public AjaxResult getOrderDetailById(@PathVariable Integer orderdetailid) {
        Orderdetail orderdetail = orderdetailService.selectByPrimaryKey(orderdetailid);
        if (orderdetail != null) {
            return AjaxResult.success("Order detail retrieved successfully", orderdetail);
        } else {
            return AjaxResult.error("Order detail not found");
        }
    }

    // 添加新的订单详情
    @PostMapping
    @ApiOperation(value = "Add a new order detail", notes = "Create a new order detail in the system")
    public AjaxResult addOrderDetail(@Valid @RequestBody Orderdetail orderdetail) {
        int result = orderdetailService.insert(orderdetail);
        if (result > 0) {
            return AjaxResult.success("Order detail added successfully");
        } else {
            return AjaxResult.error("Failed to add order detail");
        }
    }

    // 更新订单详情
    @PutMapping("/{orderdetailid}")
    @ApiOperation(value = "Update an order detail", notes = "Update an existing order detail by its ID")
    public AjaxResult updateOrderDetail(@PathVariable Integer orderdetailid, @Valid @RequestBody Orderdetail orderdetail) {
        orderdetail.setOrderdetailid(orderdetailid); // 设置更新的ID
        int result = orderdetailService.updateByPrimaryKey(orderdetail);
        if (result > 0) {
            return AjaxResult.success("Order detail updated successfully");
        } else {
            return AjaxResult.error("Failed to update order detail");
        }
    }

    // 删除订单详情
    @DeleteMapping("/{orderdetailid}")
    @ApiOperation(value = "Delete an order detail", notes = "Delete a specific order detail by its ID")
    public AjaxResult deleteOrderDetail(@PathVariable Integer orderdetailid) {
        int result = orderdetailService.deleteByPrimaryKey(orderdetailid);
        if (result > 0) {
            return AjaxResult.success("Order detail deleted successfully");
        } else {
            return AjaxResult.error("Failed to delete order detail");
        }
    }
}
